//
//  ZYZRefreshView.swift
//  MyWeiBo
//
//  Created by 牛牛很牛 on 15/12/23.
//  Copyright © 2015年 曾勇召. All rights reserved.
//

import UIKit

class ZYZRefreshControl: UIRefreshControl {
    //定义一个标示，记录是否箭头朝上
      var isUp = false
    
    
    
    //他的Y值是以tableView为参照点
    //监听frame
   override var frame : CGRect{
        didSet{
            if frame.origin.y>0 {
                return
            }
            //-60以下就转上去
            if frame.origin.y < -60  && !isUp{
                isUp = !isUp
                //箭头转上去
                freshView.romateArrow(isUp)
            
            }else if frame.origin.y > -60 && isUp
            {//箭头转下来
                isUp = !isUp
                freshView.romateArrow(isUp)
            }
      
        }
    
    }
    
    override init() {
        super.init()
        self.prepareUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
        self.tintColor = UIColor.clearColor()
    self.addSubview(freshView)
    freshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: freshView.bounds.size)
    
    }

 //MARK _ 懒加载 (类方法)
    private lazy var freshView :ZYZRefreshView = ZYZRefreshView.refreshView()
    
}


class ZYZRefreshView: UIView {
    
    @IBOutlet weak var tipView: UIView!
    @IBOutlet weak var ArrowImageView: UIImageView!
    class  func refreshView() -> ZYZRefreshView{
        let  rview = NSBundle.mainBundle().loadNibNamed("RefreshView", owner: nil, options: nil).last as! ZYZRefreshView
        return rview
    }
    
    func romateArrow(isup:Bool){
       UIView.animateWithDuration(1) { () -> Void in
        self.ArrowImageView.transform = isup ? CGAffineTransformMakeRotation(CGFloat(M_PI) - 0.001) : CGAffineTransformIdentity
        }
    }
    
}
